﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSem3.Admin
{
    public partial class WebForm24 : System.Web.UI.Page
    {
        DataClassesHeaxlthDataContext dc = new DataClassesHeaxlthDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                join();
            }
        }
        public void join()
        {
            string id = Request.QueryString["id"];
            var request = this.dc.PolicyRequestDetails.Join(dc.EmpRegisters, r => r.empno, p => p.empno, (r, p) => new
            {
                r.requestid,
                r.requestdate,
                r.empno,
                r.policyid,
                r.companyid,
                r.status,
                p.username
            }).Join(dc.policies, r => r.policyid, p => p.policyid, (r, p) => new
            {
                r.requestid,
                r.requestdate,
                r.empno,
                r.policyid,
                r.companyid,
                r.status,
                r.username,
                p.policyname,
                p.amount,
                p.emi
            }).Join(dc.CompanyDetails, r => r.companyid, c => c.companyid, (r, c) => new
            {
                r.requestid,
                r.requestdate,
                r.empno,
                r.policyid,
                r.companyid,
                r.username,
                r.policyname,
                r.status,
                r.amount,
                r.emi,
                c.companyname
            }).Join(dc.policiesonemployees, r => r.requestid, a => a.requestid, (r, a) => new {
                r.requestid,
                r.requestdate,
                r.empno,
                r.policyid,
                r.companyid,
                r.username,
                r.policyname,
                r.amount,
                r.emi,
                r.companyname,
                r.status,
                a.pstartdate,
                a.penddate
            }).Where(d => d.requestid.Equals(id) && d.status.Equals("no")).SingleOrDefault();
            if (request != null)
            {
                this.LabelUsername.Text = request.username;
                this.LabelPolicyname.Text = request.policyname;
                this.LabelAmount.Text = String.Format("{0:0.##}", request.amount);
                this.LabelEMI.Text = String.Format("{0:0.##}", request.emi);
                this.LabelStartdate.Text = string.Format("{0:d/M/yyyy}", request.pstartdate);
                this.LabelEnddate.Text = string.Format("{0:d/M/yyyy}", request.penddate);
                this.LabelCompanyname.Text = request.companyname;
            }
        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string id = Request.QueryString["id"];
                var ad = this.dc.PolicyRequestDetails.Single(a => a.requestid.Equals(id));
                int policyid = Convert.ToInt32(ad.policyid);
                int requestid = Convert.ToInt32(ad.requestid);
                string date = DateTime.Now.ToShortDateString();
                string amount = LabelAmount.Text;
                string status = "yes";
                add(policyid, requestid, date, amount, status);
                update(status);
            }
            catch
            {
                this.LabelKq.Text = "Request has been confirmed";
            }
        }
        public void add(int policyid, int requestid, string date, string amount, string status)
        {

            PolicyApprovalDetail pa = new PolicyApprovalDetail();
            pa.policyid = policyid;
            pa.requestid = requestid;
            pa.date = Convert.ToDateTime(date);
            pa.amount = Convert.ToDecimal(amount);
            pa.status = status;
            this.dc.PolicyApprovalDetails.InsertOnSubmit(pa);
            this.dc.SubmitChanges();
            ButtonAdd.Visible = false;
            this.LabelKq.Text = "Add policy to employee successful";

        }
        public void update(string status)
        {
            string id = Request.QueryString["id"];
            PolicyRequestDetail pr = this.dc.PolicyRequestDetails.Single(p => p.requestid.Equals(id));
            pr.status = status;
            this.dc.SubmitChanges();
        }
        protected void ButtonDelete_Click(object sender, EventArgs e)
        {
            string status = "fail";
            delete(status);

        }
        public void delete(string status)
        {
            string id = Request.QueryString["id"];
            PolicyRequestDetail pr = this.dc.PolicyRequestDetails.Single(p => p.requestid.Equals(id));
            pr.status = status;
            this.dc.SubmitChanges();
        }
    }
}